//
//  SN_ChangePhone_Controller.m
//  Shiyun_Newspaper
//
//  Created by zz on 17/6/27.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_ChangePhone_Controller.h"

@interface SN_ChangePhone_Controller ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UITableViewCell *cell1;
@property (nonatomic, strong) UITableViewCell *cell2;

@property (nonatomic, assign) NSInteger timeCount;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UILabel *counterLabel; //倒计时
@property (nonatomic, strong) UIButton *getVerifyBtn;
@property (nonatomic, strong) UITextField *getVerifyField; //验证码
@property (nonatomic, strong) UITextField *phoneField; //新手机号

@end

@implementation SN_ChangePhone_Controller


#pragma mark -Getter
- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, VIEW_WIDTH, VIEW_HEIGHT - SafeAreaTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = false;
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [footerView addGestureRecognizer:tap];
        footerView.backgroundColor = [UIColor colorWithHexString:@"#f4f5f7"];
        _tableView.tableFooterView = footerView;
        _tableView.rowHeight = 44;
        
    }
    
    return _tableView;
}

- (UITableViewCell *)cell1 {
    
    if (!_cell1) {
        
        _cell1 = [[UITableViewCell alloc] init];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 100, 16)];
        label.text = @"手机号";
        label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        label.font = [UIFont systemFontOfSize:16];
        [_cell1 addSubview:label];
        
        UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(92, 0, 130, 44)];
        field.placeholder = @"请输入新手机号";
        field.borderStyle = UITextBorderStyleNone;
        field.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        _phoneField = field;
        [_cell1 addSubview:field];
        
        _cell1.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return _cell1;
}

- (UITableViewCell *)cell2 {
    
    if (!_cell2) {
        _cell2 = [[UITableViewCell alloc] init];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 14, 100, 16)];
        label.text = @"验证码";
        label.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        label.font = [UIFont systemFontOfSize:16];
        [_cell2 addSubview:label];
        
        _getVerifyField = [[UITextField alloc] initWithFrame:CGRectMake(92, 0, 130, 44)];
        _getVerifyField.placeholder = @"请输入验证码";
        _getVerifyField.borderStyle = UITextBorderStyleNone;
        _getVerifyField.textColor = [UIColor colorWithHexString:@"#3b3b3b"];
        [_cell2 addSubview:_getVerifyField];
        
        _getVerifyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _getVerifyBtn.layer.cornerRadius = 6;
        [_getVerifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_getVerifyBtn setTitleColor:[UIColor colorWithHexString:@"#fb524a"] forState:UIControlStateNormal];
        _getVerifyBtn.layer.borderWidth = 1;
        _getVerifyBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        _getVerifyBtn.frame = CGRectMake(SCREENWIDTH - 15 - 84, 9, 84, 24);
        _getVerifyBtn.layer.borderColor = [UIColor colorWithHexString:@"#fb524a"].CGColor;
        [_getVerifyBtn addTarget:self action:@selector(getVerifyCode:) forControlEvents:UIControlEventTouchUpInside];
        [_cell2 addSubview:_getVerifyBtn];
        
        [_cell2 addSubview:self.counterLabel];
        
        _cell2.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    return _cell2;
}

- (UILabel *)counterLabel {
    
    if (!_counterLabel) {
        
        _counterLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREENWIDTH - 15 - 150, 15, 150, 14)];
        _counterLabel.textAlignment = NSTextAlignmentRight;
        _counterLabel.font = [UIFont systemFontOfSize:14];
        _counterLabel.textColor = [UIColor colorWithHexString:@"#979797"];
    }
    
    return _counterLabel;
}

- (NSTimer *)timer {
    
    if (!_timer) {
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTimer) userInfo:nil repeats:YES];
        
    }
    
    return _timer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navBar.navigationItem.title = @"修改绑定手机号";
    
    UIBarButtonItem *nextStepBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成 " style:UIBarButtonItemStyleDone target:self action:@selector(nextSetpAction)];
    self.navBar.navigationItem.rightBarButtonItem = nextStepBtn;
    [nextStepBtn setTitleTextAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    [self.view addSubview:self.tableView];
    
    _timeCount = 60;
}

#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        
        return self.cell1;
        
    }else{
        
        return self.cell2;
    }
}


#pragma mark -Action
- (void)nextSetpAction {
    
    if (_getVerifyField.text.length == 0) {
        
        [Utils alertViewWithMessage:@"请输入验证码"];
        return;
    }
    
    if (_phoneField.text.length == 0) {
        
        [Utils alertViewWithMessage:@"请输入手机号码"];
        return;
    }
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_verifycode_verify" forKey:@"method"];
    [dict setObject:_getVerifyField.text forKey:@"verify"];
    [dict setObject:_phoneField.text forKey:@"mobile"];
    [dict setObject:@"3" forKey:@"type"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        [self changePhoneNumber];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
    
}

- (void)getVerifyCode:(UIButton *)btn {
    
    if (_phoneField.text.length == 0) {
        
        [Utils alertViewWithMessage:@"请输入手机号码"];
        return;
    }
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    [dict setObject:_phoneField.text forKey:@"Mobile"];
    [dict setObject:@"api_VerifyCode_get" forKey:@"method"];
    [dict setObject:@"3" forKey:@"type"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        [self.view endEditing:YES];
        
        [self showHint:@"验证码已发送,请注意查收"];
        
        btn.hidden = YES;
        [self.timer fire];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
}

- (void)changeTimer {
    
    _timeCount--;
    
    if (_timeCount > 0) {
        
        _counterLabel.text = [NSString stringWithFormat:@"重新获取(%ld)",(long)_timeCount];
    }else {
        
        [_timer invalidate];
        _counterLabel.hidden = YES;
        _getVerifyBtn.hidden = NO;
        _timeCount = 60;
        _timer = nil;
    }
}


- (void)changePhoneNumber
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_usermobile_edit" forKey:@"method"];
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:_phoneField.text forKey:@"Mobile"];
    
    [Utils getWithparameters:dict showHUD:YES success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dict = (NSDictionary *)jsonObject;
        NSDictionary * personalImformation = [dict objectForKey:@"re_data"];
        [SyUserInfomation personalImformationSaveWtih:personalImformation];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:[jsonObject objectForKey:@"re_info"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 1) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

- (void)tapAction {
    
    [self.view endEditing:YES];
}


@end
